import {
	IAbility,
	IActionResult,
	IContext,
	ICtrlActionResult,
	IDECtrlAbility,
	IEditFormAbility,
	IGlobalActionHelper,
	IGridAbility,
	IHttpResponse,
	IMDCtrlAbility,
	IMDViewAbility,
	INextAction,
	IPanelDetailAbility,
	IParam,
	IUIService,
	IViewAbility,
	IViewModel,
} from '@/core';
import { deepCopy, hasFunction } from '@/core/utils';
import { router } from '@/router';
import { createVNode } from 'vue';
import { Select, SelectOption } from 'ant-design-vue';

/**
 * 全局界面行为辅助类
 */
export class GlobalActionHelper implements IGlobalActionHelper {
	/**
	 * 单例服务
	 *
	 * @protected
	 * @static
	 * @type {IGlobalActionHelper}
	 * @memberof GlobalActionHelper
	 */
	private static service: IGlobalActionHelper;

	/**
	 * 获取实例
	 *
	 * @static
	 * @return {*}  {IGlobalActionHelper}
	 * @memberof GlobalActionHelper
	 */
	static getInstance(): IGlobalActionHelper {
		if (!this.service) {
			this.service = new GlobalActionHelper();
		}
		return this.service;
	}

	/**
	 * 预置行为predefinedType与tag映射表
	 *
	 * @private
	 * @type {Map<string, string>}
	 * @memberof GlobalActionHelper
	 */
	private predefinedActionMap: Map<string, string> = new Map([
		['EDITVIEW_EXITACTION', 'Exit'],
		['EDITVIEW_SAVEANDEXITACTION', 'SaveAndExit'],
		['TREEVIEW_REFRESHPARENTACTION', 'RefreshParent'],
		['GRIDVIEW_EXPORTXMLACTION', 'ExportModel'],
		['GRIDVIEW_EXPORTACTION', 'ExportExcel'],
		['EDITVIEW_REMOVEANDEXITACTION', 'RemoveAndExit'],
		['GRIDVIEW_PRINTACTION', 'Print'],
		['EDITVIEW_NEXTRECORDACTION', 'NextRecord'],
		['GRIDVIEW_NEWROWACTION', 'NewRow'],
		['EDITVIEW_LASTRECORDACTION', 'LastRecord'],
		['EDITVIEW_PREVRECORDACTION', 'PrevRecord'],
		['GRIDVIEW_SEARCHBAR', 'ToggleFilter'],
		['EDITVIEW_SAVEANDSTARTWFACTION', 'SaveAndStart'],
		['EDITVIEW_NEWACTION', 'New'],
		['EDITVIEW_PRINTACTION', 'Print'],
		['EDITVIEW_COPYACTION', 'Copy'],
		['EDITVIEW_HELPACTION', 'Help'],
		['EDITVIEW_FIRSTRECORDACTION', 'FirstRecord'],
		['GRIDVIEW_REFRESHACTION', 'Refresh'],
		['EDITVIEW_SAVEANDNEWACTION', 'SaveAndNew'],
		['EDITVIEW_VIEWWFSTEPACTORACTION', 'ViewWFStep'],
		['EDITVIEW_SAVEACTION', 'Save'],
		['TREEVIEW_REFRESHALLACTION', 'RefreshAll'],
		['GRIDVIEW_IMPORTBAR', 'Import'],
		['GRIDVIEW_ROWEDITACTION', 'ToggleRowEdit'],
		['GRIDVIEW_NEWACTION', 'New'],
		['GRIDVIEW_EDITACTION', 'Edit'],
		['GRIDVIEW_HELPACTION', 'Help'],
		['EDITVIEW_REFRESHACTION', 'Refresh'],
		['GRIDVIEW_REMOVEACTION', 'Remove'],
		['GRIDVIEW_COPYACTION', 'Copy'],
		['GRIDVIEW_VIEWACTION', 'View'],
		['GRIDVIEW_SAVEROWACTION', 'SaveRow'],
		['APP_LOGIN', 'Login'],
		['APP_LOGOUT', 'Logout'],
		['DATA_CREATEOBJECT', 'DataCreate'],
		['DATA_REMOVEOBJECT', 'DataRemove'],
		['DATA_SAVECHANGES', 'DataSaveChange'],
		['DATA_CANCELCHANGES', 'DataCancelChange'],
		['DATA_SYNCHRONIZE', 'DataSynchronize'],
	]);

	/**
	 * 通过传入tag获取行为方法名称（兼容老写法，识别新增属性predefinedType）
	 *
	 * @private
	 * @param {string} predefinedType
	 * @return {*}  {string}
	 * @memberof GlobalActionHelper
	 */
	private getActionMethodByTag(predefinedType: string): string {
		if (this.predefinedActionMap.get(predefinedType)) {
			return this.predefinedActionMap.get(predefinedType) as string;
		} else {
			return predefinedType;
		}
	}

	/**
	 * 执行全局界面行为
	 *
	 * @template T 能力泛型
	 * @param {string} predefinedType 预置行为类型
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @param {IParam[]} data 数据
	 * @param {MouseEvent} event 鼠标源事件
	 * @param {T} ability 能力
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async executeUIAction<T extends IAbility>(
		predefinedType: string,
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		const self: any = this;
		const actionTag = this.getActionMethodByTag(predefinedType);
		if (self[actionTag] && self[actionTag] instanceof Function) {
			return await self[actionTag](context, viewParams, data, event, ability, otherParams);
		} else {
			console.warn(`${actionTag} 暂未实现`);
			return { ok: false, data: null };
		}
	}

	/**
	 * 帮助
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Help<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '帮助暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 保存
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Save<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'save')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).save(viewParams);
		return result;
	}

	/**
	 * 退出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Exit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'closeView')) {
			return { ok: false, data: null };
		}
		return (ability as any).closeView();
	}

	/**
	 * 保存并退出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndExit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		const result: IActionResult = await this.Save(
			context,
			viewParams,
			data,
			event,
			ability
		);
		if (result.ok) {
			if (otherParams && otherParams.nextAction) {
				return this.executeNextUIAction(context, viewParams, result.data || [], event, ability, otherParams.nextAction)
			}else{
				if (App.getProjectSetting().cache && ability.getModel()) {
					App.getGlobalNotificationHelper().notice(ability.getModel().entityCodeName, 'refresh');
				}
				return (ability as any).closeView();
			}
		} else {
			return { ok: false, data: null };
		}
	}

	/**
	 * 保存并新建
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndNew<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '保存并新建暂未支持');
		const result = await this.Save(context, viewParams, data, event, ability);
		if (result.ok) {
			return await this.New(context, viewParams, data, event, ability);
		}
		return { ok: false, data: null };
	}

	/**
	 * 保存行
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveRow<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'save')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).save(viewParams);
		return result;
	}

	/**
	 * 查看
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async View<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'openData')) {
			return { ok: true, data: null };
		}
		(ability as any).openData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 打印
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Print<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '打印暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 当前流程步骤
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ViewWFStep<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '打印暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 导出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ExportExcel<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'exportExcel')) {
			return { ok: true, data: null };
		}
		const { srfactionparams } = viewParams;
		(ability as unknown as IMDCtrlAbility).exportExcel(
			srfactionparams || { type: 'activatedPage' }
		);
		return { ok: true, data: null };
	}

	/**
	 * 第一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async FirstRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '第一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 最后一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async LastRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '最后一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 前一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async PrevRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '前一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 后一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async NextRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('警告', '后一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 切换过滤器
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ToggleFilter<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'toggleFilter')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IMDViewAbility).toggleFilter();
		return { ok: true, data: null };
	}

	/**
	 * 开始流程
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndStart<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (ability && !hasFunction(ability, 'wfStart')) {
			return { ok: false, data: null };
		}
		const formAbility = ability as unknown as IEditFormAbility;
		try {
			const entitySerivice = await App.getDataService(
				formAbility.getModel().entityCodeName
			);
			if (!entitySerivice) {
				return { ok: false, data: null };
			}
			const localData: IParam = {};
			const tempContext: IContext = deepCopy(context);
			const response: IHttpResponse = await entitySerivice.getStandWorkflow(
				tempContext
			);
			if (response.success && response.data.length > 0) {
				const startWorkFlow = async (
					items: IParam[],
					localData: IParam,
					useWFProxyApp: boolean = false
				) => {
					const result: ICtrlActionResult = await formAbility.wfStart(items, useWFProxyApp ? undefined : localData);
                    if (result.ok) {
                        if (otherParams && otherParams.nextAction) {
                            return this.executeNextUIAction(context, viewParams, result.data || [], event, ability, otherParams.nextAction)
                        } else {
                            if (App.getProjectSetting().cache) {
                                App.getGlobalNotificationHelper().notice(formAbility.getModel().entityCodeName, 'refresh');
                            }
                            return formAbility.closeView();
                        }
                    }
                    return { ok: false, data: null };
				};
				//	打开启动视图
				const openStartView = async (item: IParam, localData: IParam) => {
					if (item['modelversion']) {
						const model: IViewModel = formAbility.viewCtx.view.getModel();
						const viewName = model.name;
						const viewDetail = App.getAppViewConfig()[viewName];
						const viewRefs: IParam =
							viewDetail && viewDetail.viewRefs ? viewDetail.viewRefs : {};
						//	是否使用工作流代理应用
						const useWFProxyApp = viewDetail
							? viewDetail.useWFProxyApp === true
							: false;
						const targetViewName = viewRefs[`WFSTART@${item['modelversion']}`];
						if (targetViewName) {
							const targetViewDetail = App.getAppViewConfig()[targetViewName];
							const tempContext = deepCopy(context);
							const tempViewParams = {
								actionView: `WFSTART@${item['modelversion']}`,
								actionForm: item['process-form'],
							};
							App.getOpenViewHelper()
								.openModal(targetViewDetail, tempContext, tempViewParams)
								.then((result: IActionResult) => {
									const tempSubmitData =
										data && data.length > 0 ? deepCopy(data[0]) : {};
									if (result.ok && result.data && result.data.length > 0) {
										const resultData = result.data[0];
										const tempData: IParam = {};
										Object.keys(resultData).forEach((key: string) => {
											if (
												resultData[key] ||
												resultData[key] === 0 ||
												resultData[key] === false
											) {
												tempData[key] = resultData[key];
											}
										});
										Object.assign(tempSubmitData, tempData);
									}
									return startWorkFlow([tempSubmitData], localData, useWFProxyApp);
								});
						} else {
							return startWorkFlow(data, localData, useWFProxyApp);
						}
					} else {
						return startWorkFlow(data, localData);
					}
				};
				const targetData = response.data;
				targetData.forEach((element: IParam) => {
					Object.assign(element, {
						value: element.definitionkey,
						label: element.definitionname,
					});
				});
				if (!targetData || targetData.length === 0) {
					return { ok: false, data: null };
				}
				if (targetData.length > 1) {
					App.getDialogHelper().custom({
						title: App.ts('app.common.selectversioning'),
						content: () => {
							return createVNode(
								Select,
								{
									onChange: (value: string) => {
										Object.assign(localData, { processDefinitionKey: value });
									},
								},
								targetData.map((item: IParam) => {
									return createVNode(SelectOption, {
										value: item.value,
										title: item.label,
									});
								})
							);
						},
                        okText: App.ts('app.common.confirm'),
                        cancelText: App.ts('app.common.cancel'),
						ok: () => {
							const targetItem = targetData.find(
								(element: IParam) =>
									element.definitionkey === localData.processDefinitionKey
							);
							openStartView(targetItem, localData);
						},
					});
				} else {
					openStartView(targetData[0], {
						processDefinitionKey: targetData[0]['definitionkey'],
					});
				}
			}
		} catch (error) {
			return { ok: false, data: null };
		}
		return { ok: false, data: null };
	}

	/**
	 * 拷贝
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Copy<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!data || data.length === 0) {
			return { ok: true, data: null };
		}
		Object.assign(viewParams, { copymode: true });
		return await this.New(context, viewParams, data, event, ability);
	}

	/**
	 * 删除
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Remove<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'remove')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).remove(data);
		return result;
	}

	/**
	 * 删除并关闭
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RemoveAndExit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		const result: IActionResult = await this.Remove(
			context,
			viewParams,
			data,
			event,
			ability
		);
		if (result.ok) {
			if (otherParams && otherParams.nextAction) {
				return this.executeNextUIAction(context, viewParams, result.data || [], event, ability, otherParams.nextAction)
			}else{
				if (App.getProjectSetting().cache && ability.getModel()) {
					App.getGlobalNotificationHelper().notice(ability.getModel().entityCodeName, 'refresh');
				}
				return (ability as any).closeView();
			}
		} else {
			return { ok: false, data: null };
		}
	}

	/**
	 * 刷新
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Refresh<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refresh')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refresh(viewParams);
		return result;
	}

	/**
	 * 刷新父
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RefreshParent<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refreshParentNode')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refreshParentNode();
		return result;
	}

	/**
	 * 刷新全部
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RefreshAll<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refreshAllNode')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refreshAllNode();
		return result;
	}

	/**
	 * @description 数据导出
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Import<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'importExcel')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IGridAbility).importExcel(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 新建
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async New<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'newData')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IDECtrlAbility).newData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 编辑
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Edit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'openData')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IDECtrlAbility).openData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 新建行
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async NewRow<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'newRow')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IGridAbility).newRow(data);
		return { ok: true, data: null };
	}

	/**
	 * 行编辑
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ToggleRowEdit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'toggleRowEdit')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IGridAbility).toggleRowEdit();
		return { ok: true, data: null };
	}

	/**
	 * 登录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Login<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: IViewAbility,
        otherParams?: IParam
	): Promise<void> {
		const layoutData = data.length > 0 ? data[0] : {};
		const layoutModelDetails: any = ability.getLayoutModelDetails() || {};
		const handlePanelValueChange = ability.handlePanelValueChange;
		const userNameKey: string =
			Object.keys(layoutModelDetails).find((key: string) =>
				Object.is(layoutModelDetails[key].predefinedType, 'AUTH_USERID')
			) || '';
		const passwordKey: string =
			Object.keys(layoutModelDetails).find((key: string) =>
				Object.is(layoutModelDetails[key].predefinedType, 'AUTH_PASSWORD')
			) || '';
		const messageKey: string =
			Object.keys(layoutModelDetails).find((key: string) =>
				Object.is(layoutModelDetails[key].predefinedType, 'AUTH_LOGINMSG')
			) || '';
		if (!userNameKey || !passwordKey) {
			if (messageKey) {
				handlePanelValueChange(messageKey, '用户名或密码未配置！');
			} else {
				App.getNotificationHelper().warning('警告', '用户名或密码未配置！');
			}
		}
		const loginname: any = layoutData[userNameKey];
		const password: any = layoutData[passwordKey];
		if (!loginname || !password) {
			if (messageKey) {
				handlePanelValueChange(messageKey, '请输入用户名与密码');
			} else {
				App.getNotificationHelper().warning('警告', '请输入用户名与密码');
			}
		}
		App.login(loginname, password);
	}

	/**
	 * 登出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Logout<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<void> {
		App.logout().then(() => {
			router.push({
				path: '/login',
				query: { redirect: window.location.hash.replace('#', '') },
			});
		});
	}

	/**
	 * 建立数据
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataCreate<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		const result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'createData')) {
			await (ability as unknown as IPanelDetailAbility).createData(
				context,
				viewParams,
				data
			);
		}
		return result;
	}

	/**
	 * 删除数据
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataRemove<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'removeData')) {
			await (ability as unknown as IPanelDetailAbility).removeData(
				context,
				viewParams,
				data
			);
		} else {
			result = await this.Remove(context, viewParams, data, event, ability);
		}
		return result;
	}

	/**
	 * 保存变更
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataSaveChange<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'saveData')) {
			await (ability as unknown as IPanelDetailAbility).saveData(
				context,
				viewParams,
				data
			);
		} else {
			result = await this.Save(context, viewParams, data, event, ability);
		}
		return result;
	}

	/**
	 * 取消变更
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataCancelChange<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: IViewAbility,
        otherParams?: IParam
	): Promise<void> {
		const model = ability.getModel();
		if (model.viewType && model.viewType === 'APPLOGINVIEW') {
			const layoutData: IParam = data.length > 0 ? data[0] : {};
			const layoutModelDetails = ability.getLayoutModelDetails() || {};
			const handlePanelValueChange = ability.handlePanelValueChange;
			const length = Object.keys(layoutData).length;
			for (let i = length - 1; i >= 0; i--) {
				const name = Object.keys(layoutData)[i];
				if (
					layoutData &&
					layoutData[name] &&
					!Object.is(layoutModelDetails[name].itemType, 'CONTAINER')
				) {
					handlePanelValueChange(name, null);
				}
			}
		} else {
			if (ability.closeView && ability.closeView instanceof Function) {
				ability.closeView();
			}
		}
	}

	/**
	 * 数据同步
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataSynchronize<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        otherParams?: IParam
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (
			!ability ||
			(!hasFunction(ability, 'refresh') &&
				!hasFunction(ability, 'refreshDataArea'))
		) {
			return result;
		}
		if (hasFunction(ability, 'refresh')) {
			result = await (ability as any).refresh(viewParams);
		}
		if (hasFunction(ability, 'refreshDataArea')) {
			result = await (
				ability as unknown as IPanelDetailAbility
			).refreshDataArea(viewParams);
		}
		return result;
	}

    /**
     * 执行下一步界面行为
     *
     * @template T
     * @param {IContext} context
     * @param {IParam} viewParams
     * @param {IParam[]} data
     * @param {MouseEvent} event
     * @param {T} ability
     * @param {INextAction} nextAction
     * @return {*}  {Promise<IActionResult>}
     * @memberof GlobalActionHelper
     */
    public async executeNextUIAction<T extends IAbility>(
        context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
        nextAction: INextAction
    ): Promise<IActionResult> {
        let result: IActionResult = { ok: true, data: null };
        if (nextAction.predefinedType) {
			return this.executeUIAction(
				nextAction.name,
				context,
				viewParams,
				data,
				event,
				ability
			);
		}
		if (nextAction.actionDeCodeName) {
            const uiService: IUIService = await App.getUIService(
				nextAction.actionDeCodeName,
				context
			);
			if (!uiService) {
				return { ok: false, data: null };
			}
			return await uiService.executeUIAction(
                nextAction.name,
                context,
                viewParams,
                data,
                event,
                ability
			);
		}
        return result;
    }
}